package addresslist;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author mwh
 */
public class Names {

    static String newline = System.getProperty("line.separator");
    
    private List<String> names;


    public Names() {
        names = new ArrayList<String>();
    }


    public void addName(String name) {
        names.add(name);
    }


    public List<String> getNames() {
        return names;
    }


    public List<String> getSortedNames() {
        sortNames();
        return names;
    }


    public void sortNames() {
        boolean changed = true;
        while (changed) {
            changed = false;
            for (int i = 0; i < (names.size() - 2); i++) {
                String a = names.get(i);
                String b = names.get(i + 1);
                if (a.compareTo(b) > 0) {
                    names.set(i, b);
                    names.set(i + 1, a);
                    changed = true;
                }
            }
        }
    }


    @Override
    public String toString() {
        String str = "";
        for (int i = 0; i < names.size(); i++) {
            str = append(str, names.get(i)+newline);

        }
        return str;
    }
    
    private String append(String a, String b) {
        return a + b;
    }


//    @Override
//    public String toString() {
//        StringBuffer str = new StringBuffer();
//        for (int i = 0; i < names.size(); i++) {
//            str.append(names.get(i)+newline);
//        }
//        return str.toString();
//    }
}
